package edu.scau.misp.shop.web;

import java.util.List;



import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import edu.scau.misp.core.web.BaseController;
import edu.scau.misp.shop.entity.Customer;
import edu.scau.misp.shop.service.CustomerService;


/**
 * 客户controller
 * @author DCJ
 * 
 */
@Controller
@RequestMapping("shop/customer")
public class CustomerController extends BaseController{

	@Autowired
	private CustomerService customerService;

	/**
	 * 默认页面
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "shop/customerList";
	}
	
	/**
	 * 获取客户类型json
	 */
	@RequestMapping(value="json",method = RequestMethod.GET)
	@ResponseBody
	public List<Customer> customerList(HttpServletRequest request) {
		List<Customer> customerList=customerService.getAll();
		return customerList;
	}
	
	/**
	 * 添加客户跳转
	 * 
	 * @param model
	 */
	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("customer", new Customer());
		model.addAttribute("action", "create");
		return "shop/customerForm";
	}

	/**
	 * 添加客户
	 * 
	 * @param customer
	 * @param model
	 */
	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ResponseBody
	public String create(@Valid Customer customer, Model model) {
		customerService.save(customer);
		return "success";
	}

	/**
	 * 修改客户跳转
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Integer id, Model model) {
		model.addAttribute("customer", customerService.get(id));
		model.addAttribute("action", "update");
		return "shop/customerForm";
	}

	/**
	 * 修改客户
	 * 
	 * @param customer
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public String update(@Valid @ModelAttribute @RequestBody Customer customer,Model model) {
		customerService.update(customer);
		return "success";
	}

	/**
	 * 删除客户
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "delete/{id}")
	@ResponseBody
	public String delete(@PathVariable("id") Integer id) {
		customerService.delete(id);
		return "success";
	}
	
	@ModelAttribute
	public void getCustomer(@RequestParam(value = "id", defaultValue = "-1") Integer id,Model model) {
		if (id != -1) {
			model.addAttribute("customer", customerService.get(id));
		}
	}
}
